﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using ZhangBY.Entity;

using ZhangBY.Config;

namespace ZhangBY.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        /// <summary>
        /// 获取分类表（proclass）记录
        /// </summary>
        /// <returns></returns>
        public DataSet GetProClass()
        {
            return DbHelper.ExecuteDataset(CommandType.Text, string.Format("SELECT * FROM [dbo].[{0}proclass]", BaseConfigs.GetTablePrefix));
        }

        ///// <summary>
        ///// 获取分类当前节点和所有父节点记录
        ///// </summary>
        ///// <returns></returns>
        //public DataSet GetAllProClassParentInfo(int id)
        //{
        //    DbParameter getParms=DbHelper.MakeInParam("@id",(DbType)SqlDbType.Int,int.MaxValue,id);
        //    return DbHelper.ExecuteDataset(CommandType.StoredProcedure, "[dbo].[sp_getAllClsParent]", getParms);
        //}

        /// <summary>
        /// 更新proCLASS记录
        /// 根据ID更新
        /// </summary>
        /// <returns></returns>
        public int UpdateProClass(ProClassInfo proClsInfo)
        {
            DbParameter[] getParms ={
                                       DbHelper.MakeInParam("@id",(DbType)SqlDbType.Int,int.MaxValue,proClsInfo.Id),
                                       DbHelper.MakeInParam("@classpid",(DbType)SqlDbType.Int,int.MaxValue,proClsInfo.Pid),
                                       DbHelper.MakeInParam("@classname",(DbType)SqlDbType.NChar,20,proClsInfo.Name),
                                       DbHelper.MakeInParam("@classkey",(DbType)SqlDbType.NChar,40,proClsInfo.Key),
                                       DbHelper.MakeInParam("@classdescp",(DbType)SqlDbType.NChar,40,proClsInfo.Description),
                                       DbHelper.MakeInParam("@date",(DbType)SqlDbType.DateTime,proClsInfo.Date.Length,proClsInfo.Date)
                                    };
            string commandText = "UPDATE [dbo].[zby_proclass] SET [classid]=@classpid,[classname]=@classname,[classkey]=@classkey,[classdescp]=@classdescp,[date]=@date WHERE [id]=@id";
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getParms);
        }
        /// <summary>
        /// 更新子节点数
        /// isADD为真是增加子节点反之减少子节点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isAdd">是否增加</param>
        /// <returns></returns>
        public int UpdateProClassChildCount(int id,bool isAdd)
        {
            DbParameter getParms = DbHelper.MakeInParam("@id",(DbType)SqlDbType.Int,int.MaxValue,id);
            string commandText = string.Format("UPDATE [dbo].[{0}conclass] SET [classchildcount]={1} WHERE [id]=@id", BaseConfigs.GetTablePrefix, isAdd ? "[classchildcount]+1" : "[classchildcount]-1");
            return DbHelper.ExecuteNonQuery(CommandType.Text,commandText,getParms);
        }
        /// <summary>
        /// 向CONCLASS表插入一条新分类
        /// </summary>
        /// <returns></returns>
        public int InsertProClass(ProClassInfo proClsInfo)
        {
            DbParameter[] getParms = {
                                       DbHelper.MakeInParam("@classpid",(DbType)SqlDbType.Int,int.MaxValue,proClsInfo.Pid),
                                       DbHelper.MakeInParam("@classname",(DbType)SqlDbType.NChar,20,proClsInfo.Name),
                                       DbHelper.MakeInParam("@classkey",(DbType)SqlDbType.NChar,40,proClsInfo.Key),
                                       DbHelper.MakeInParam("@classdescp",(DbType)SqlDbType.NChar,40,proClsInfo.Description),
                                       DbHelper.MakeInParam("@date",(DbType)SqlDbType.DateTime,proClsInfo.Date.Length,proClsInfo.Date)
                                     };
            string commandText = string.Format("INSERT INTO[dbo].[{0}conclass] ([classname],[classpid],[classkey],[classchildcount],[classleaf],[classpath],[classdescp],[date])VALUES(@classname,@classpid,@classkey,0,0,0,@classdescp,@date)",BaseConfigs.GetTablePrefix);
            return DbHelper.ExecuteNonQuery(CommandType.Text,commandText,getParms);
        }


    }
}
